//
//  MainTabBarViewController.m
//  EducationOID
//
//  Created by slz on 2017/5/9.
//  Copyright © 2017年 bnsr. All rights reserved.
//

#import "MainTabBarViewController.h"
#import "OINavigationController.h"
#import "HomeViewController.h"
#import "OtherViewController.h"
#import "UserInfoViewController.h"
#import "OILoginViewController.h"
#import "AppDelegate.h"

@interface MainTabBarViewController ()<UITabBarControllerDelegate,UINavigationControllerDelegate>

@end

@implementation MainTabBarViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initNavigationController];
    // Do any additional setup after loading the view.
}

-(instancetype)init
{
    self = [super init];
    if(self){
        self.delegate = self;
        [self initNavigationController];
    }
    return self;
}

- (void)tabBarController:(UITabBarController *)tabBarController didSelectViewController:(UIViewController *)viewController{
    if (tabBarController.selectedIndex == 2){
        if (![MFWSdkEnvironment sharedInstance].hasLogin){
            [MainTabBarViewController presentLoginViewControllerFromTabController];
        }
    }
}

- (BOOL)tabBarController:(UITabBarController *)tabBarController shouldSelectViewController:(UIViewController *)viewController{
    if ([viewController isKindOfClass:[OINavigationController class]]){
        OINavigationController * navController = (OINavigationController *)viewController;
        UIViewController * firstController = navController.viewControllers.firstObject;
        if ([firstController isKindOfClass:[UserInfoViewController class]]){
            if ([MFWSdkEnvironment sharedInstance].hasLogin){
                return YES;
            }else{
                [MainTabBarViewController presentLoginViewControllerFromTabController];
                return NO;
            }
        }
    }
    return YES;
}

+ (void)presentLoginViewControllerFromTabController{
    OILoginViewController * loginController = [[OILoginViewController alloc]init];
    OINavigationController * navController = [[OINavigationController alloc]initWithRootViewController:loginController];
    @weakify(navController);
    loginController.dismiss = ^ (BOOL isSuccessed){
        @strongify(navController);
        [navController dismissViewControllerAnimated:YES completion:NULL];
        if (isSuccessed){
            [AppDelegate sharedInstance].mainPageController.selectedIndex = 2;
        }
    };
    [[AppDelegate sharedInstance].mainPageController presentViewController:navController animated:YES completion:NULL];
}


- (void)initNavigationController{
    OINavigationController *nav1 = [self createClass:[HomeViewController class]
                                                name:@"主页"
                                             imgName:@"tab_home"
                                       imgSelectName:@"tab_home_click"];

    OINavigationController *nav2 = [self createClass:[OtherViewController class]
                                                name:@"应用"
                                             imgName:@"tab_app"
                                       imgSelectName:@"tab_app_click"];
    
    OINavigationController *nav3 = [self createClass:[UserInfoViewController class]
                                                name:@"个人"
                                             imgName:@"tab_user"
                                       imgSelectName:@"tab_user_click"];
    
    self.viewControllers = [NSArray arrayWithObjects:
                            nav2,
                            nav1,
                            nav3,
                            nil];
    
    //    [self.view setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"all_bg.png"]]];
//    [[UITabBar appearance] setTintColor:YELLOWCOLOR];
//    [[UITabBar appearance] setBackgroundImage:[UIImage imageWithColor:[UIColor colorWithRGB:0xf8f8f8 alpha:1]]];
//
//    
    [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor blackColor]}
                                             forState:UIControlStateNormal];
    [[UITabBarItem appearance] setTitleTextAttributes:@{ NSForegroundColorAttributeName : [UIColor colorWithRGB:0xbe5027]}
                                             forState:UIControlStateSelected];
    
    
//    UIImage *image = [UIImage imageWithColor:[UIColor blueColor]];
//    
//    image = [image imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal];
//    
//    [UINavigationBar appearance].backIndicatorImage = image;
//    
//    [UINavigationBar appearance].backIndicatorTransitionMaskImage = image;
    //updatMsgCount
    
    [UINavigationBar appearance].tintColor = kBack_Color_Default;
    
    
    self.selectedIndex = 1;
}

- (OINavigationController*)createClass:(Class)vcClass
                                  name:(NSString*)name
                               imgName:(NSString*)imgName
                         imgSelectName:(NSString*)selectName
{
    UIViewController *vc = [[vcClass alloc] init];
    vc.tabBarItem = [self createUITabBarItem:name
                                         img:imgName
                                 selectedImg:selectName];
    
    OINavigationController *nav = [[OINavigationController alloc] initWithRootViewController:vc];
    return nav;
}

-(UITabBarItem*)createUITabBarItem:(NSString*)title img:(NSString*)imgName selectedImg:(NSString*)selectimg{
    UITabBarItem *item = [[UITabBarItem alloc] initWithTitle:title image:[[UIImage imageNamed:imgName] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] selectedImage:[[UIImage imageNamed:selectimg]imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal]];
    return item;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
